import request from '../../utils/https.js';
import util from '../../utils/util.js';
var userId;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value: 3,
    isGetPhone: false,
    prompt: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //检查服务是否暂停
    request._get(getApp().Api_url + 'system', {}, function (res) {
      if (res.data.errMsg == 'OK') {
        var flag = res.data.data.on;
        if (!flag) {
          wx.reLaunch({
            url: '/pages/stop1/stop1'
          })
          return false;
        }
      }
    }, function () {});
    //邀请好友
    request._get(getApp().Api_url + 'user/friendPic', {}, function (res) {
      if (res.data.errMsg == 'OK') {
        console.log(res.data.data)
        that.setData({
          picUrl: res.data.data.picUrl
        })

      }
    }, function () {})
    const that = this;
    /*     let phone = wx.getStorageSync('phone');
        if (!phone) {
          console.log('没有手机号')
          that.setData({
            isGetPhone: true
          })
        } */
      wx.getStorageSync('fromUser'),
        that.setData({
          nickName: wx.getStorageSync('username'),
          avatarUrl: wx.getStorageSync('usericon')
        })
    // //更新用户信息
    // wx.getUserInfo({
    //   success: function (res) {
    //     var userInfo = res.userInfo
    //     var nickName = userInfo.nickName
    //     var avatarUrl = userInfo.avatarUrl
    //     var gender = userInfo.gender //性别 0：未知、1：男、2：女
    //     var province = userInfo.province
    //     var city = userInfo.city
    //     var country = userInfo.country
    //     //wx.setStorageSync('userInfo', userInfo);
    //
    //   }
    // })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  prompt() {
    const that = this;
    request._get(getApp().Api_url + 'notice/has', {
      userId: userId
    }, function (res) {

      if (res.data.code == 200) {
        if (res.data.data > 0) {
          that.setData({
            prompt: true
          })
        } else {
          that.setData({
            prompt: false
          })
        }

      }
    }, function () {});
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const that = this;
    userId = wx.getStorageSync("userId");
    let userInfo = wx.getStorageSync('userInfo');
    if (userId == '' || userId == null || userId == 'undefined') {
      wx.reLaunch({
        url: '/pages/login/login?path=/pages/my/my'
      })
      return;
    }
    that.setData({
      userInfo: userInfo
    })


    //系统消息
    that.prompt();
    //用户信息
    request._get(getApp().Api_url + 'user/getMainInfo', {
      userId: userId,
    }, function (res) {
      if (res.data.errMsg == 'OK') {
        console.log('****==>', res.data.data)
        let mainInfo = res.data.data;
        let className;
        if (mainInfo.rankNum == 0) {
          className = 'green';
        } else if (mainInfo.rankNum == 1) {
          className = 'pink';
        } else if (mainInfo.rankNum == 2) {
          className = 'purple';
        } else if (mainInfo.rankNum == 3) {
          className = 'golden';
        }
        that.setData({
          mainInfo: mainInfo,
          className: className
        })

      }
    }, function () {})

    request._get(getApp().Api_url + 'rank/toAuth', {
      userId: userId
    }, function (res) {

      if (res.data.code == 200) {
        console.log('页面初始化====>', res.data.data)
        let PartnerData = res.data.data;
        let agentType = PartnerData.result; //-1无任何审核记录 0成功 1审核失败 2待审核 3审核成功
        let reason = PartnerData.reason; //失败原因
        console.log('====审核状态===>', agentType)
        that.setData({
          PartnerData: PartnerData,
          agentType: agentType
        })

      }
    }, function () {});
  },



  goAgent() {
    let userId = wx.getStorageSync("userId");
    if (userId == '' || userId == null || userId == 'undefined') {
      wx.reLaunch({
        url: '/pages/login/login?path=/pages/list/list'
      })
      return;
    } else {
      wx.navigateTo({
        url: '/packageB/pages/agent/agent'
      })
    }
  },
  getMy() {
    userId = wx.getStorageSync("userId");
    if (userId == '' || userId == null || userId == 'undefined') {
      wx.navigateTo({
        url: '/pages/login/login?path=/pages/my/my'
      })
    }
  },
  goQyxq() {
    userId = wx.getStorageSync("userId");
    if (userId == '' || userId == null || userId == 'undefined') {
      wx.navigateTo({
        url: '/pages/login/login?path=/pages/my/my'
      })
    } else {
      wx.navigateTo({
        url: '/packageB/pages/qyxq/qyxq'
      })
    }
  },
  goQbfs() {
    userId = wx.getStorageSync("userId");
    if (userId == '' || userId == null || userId == 'undefined') {
      wx.navigateTo({
        url: '/pages/login/login?path=/pages/my/my'
      })
    } else {
      wx.navigateTo({
        url: '/pages/qbfs/qbfs'
      })
    }
  },
  goEarnings() {
    userId = wx.getStorageSync("userId");
    if (userId == '' || userId == null || userId == 'undefined') {
      wx.navigateTo({
        url: '/pages/login/login?path=/pages/my/my'
      })
    } else {
      wx.navigateTo({
        url: '/pages/earnings/earnings'
      })
    }
  },
  goBefore() {
    userId = wx.getStorageSync("userId");
    if (userId == '' || userId == null || userId == 'undefined') {
      wx.navigateTo({
        url: '/pages/login/login?path=/pages/my/my'
      })
    } else {
      wx.navigateTo({
        url: '/pages/before/before'
      })
    }
  },
  goZsfs() {
    userId = wx.getStorageSync("userId");
    if (userId == '' || userId == null || userId == 'undefined') {
      wx.navigateTo({
        url: '/pages/login/login?path=/pages/my/my'
      })
    } else {
      wx.navigateTo({
        url: '/pages/zsfs/zsfs?title=直属粉丝'
      })
    }
  },

  getPhoneNumber(e) {
    const that = this;
    util._getPhoneNumber(e, that)
  },
  onShut() {
    const that = this;
    that.setData({
      isGetPhone: false
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    const that = this;
    userId = wx.getStorageSync("userId");

    if (userId == '' || userId == null || userId == 'undefined') {
      wx.reLaunch({
        url: '/pages/login/login?path=/pages/my/my',
      })
      return;
    } else {
      if (res.from === 'button') {
        // 来自页面内转发按钮
        console.log('来自页面内转发按钮 userId===>', userId)
        return {
          title: '快美甄选',
          path: '/pages/list/list?userId=' + userId
        }
      } else {
        console.log('非按钮转发 userId===>', userId)
        return {
          title: '快美甄选',
          path: '/pages/list/list?userId=' + userId
        }
      }

    }
  },
  myQrcode() {
    const that = this;
    wx.navigateTo({
      url: '/pages/yqhy/yqhy',
    })
  }
})